<template>
    <div>
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="手机号" 
                    :rules="[{ pattern : pattern , required: true, message: '请填写11位手机号' }]" />

                <van-field v-model="sms" center clearable label="短信验证码" 
                :rules="[{ required: true, message: '请输入6位验证码' }]">
                    <template #button>
                        <van-button size="small" type="primary" @click="getsms" :disabled="time>0 ? true : false">{{time>0 ? time + '秒后获取' : '获取验证码'}}</van-button>
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>

    </div>
</template>

<script setup>
import { ref } from 'vue';

let pattern = /^1\d{10}/
let sms = ref('')
let username = ref('')


let time = ref(0)
let getsms = () =>{
    sms.value = Math.random().toString().slice(2,8)
    time.value = 10

    let time1 = setInterval(()=>{
        time.value--
        console.log(time.value);
        
        if(time.value == 0){
            clearInterval(time1)
        }
    },1000)
}


</script>

<style lang="scss" scoped></style>